package com.usesion.controller;

import com.usesion.base.entity.JsonResult;
import com.usesion.base.controller.BaseController;
import com.usesion.base.page.PageDataInfo;
import com.usesion.entity.Role;
import com.usesion.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc 角色信息
 * @author XT
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    private String prefix = "system/role";

    @Autowired
    private RoleService roleService;

    @RequiresPermissions("system:role:view")
    @GetMapping()
    public String role() {
        return prefix + "/role";
    }

    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    @ResponseBody
    public PageDataInfo list(Role role) {
        startPage();
        List<Role> list = roleService.selectRoleList(role);
        return getPageData(list);
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("system:role:add")
    @GetMapping("/add")
    public String add(Model model) {
        return prefix + "/add";
    }

    /**
     * 修改角色
     */
    @RequiresPermissions("system:role:edit")
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Integer roleId, Model model) {
        Role role = roleService.selectRoleById(roleId);
        model.addAttribute("role", role);
        return prefix + "/edit";
    }

    /**
     * 保存角色
     */
    @RequiresPermissions("system:role:save")
    @PostMapping("/save")
    @Transactional(rollbackFor=Exception.class)
    @ResponseBody
    public JsonResult save(Role role) {
        if (roleService.saveRole(role) > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    @RequiresPermissions("system:role:remove")
    @RequestMapping("/remove/{roleId}")
    @Transactional(rollbackFor=Exception.class)
    @ResponseBody
    public JsonResult remove(@PathVariable("roleId") Integer roleId) {
        Role role = roleService.selectRoleById(roleId);
        if (role == null) {
            return JsonResult.failure("角色不存在");
        }
        if (roleService.selectCountUserRoleByRoleId(roleId) > 0) {
            return JsonResult.failure("角色已分配,不能删除");
        }
        if (roleService.deleteRoleById(roleId) > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    @RequiresPermissions("system:role:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public JsonResult batchRemove(@RequestParam("ids[]") Integer[] ids) {
        int rows = roleService.batchDeleteRole(ids);
        if (rows > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(Role role) {
        String uniqueFlag = "0";
        if (role != null) {
            uniqueFlag = roleService.checkRoleNameUnique(role);
        }
        return uniqueFlag;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree")
    public String selectMenuTree() {
        return prefix + "/tree";
    }

}